package com.wxzz.elearing.course.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.StudyPlanDto;
import com.wxzz.elearing.course.dto.StudyPlanInsDto;
import com.wxzz.elearing.course.entity.StudyPlan;
import com.wxzz.elearing.course.entity.StudyPlanUnit;
import com.wxzz.elearing.course.service.IStudyPlanService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx-study-plan")
@Api(tags = "学习计划")
public class WxStudyPlanController {

    @Autowired
    private IStudyPlanService studyPlanService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "学习计划列表(小程序)", notes = "学习计划列表(小程序)", httpMethod = "POST",tags = "小程序")
    @RequestMapping(value = "wxListStudyPlan", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<StudyPlan>>> wxListStudyPlan(@RequestHeader String token, @RequestBody StudyPlanDto studyPlanDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return studyPlanService.wxListStudyPlan(jsonObject.getLong("id"), jsonObject.getLong("enterpriseId"), studyPlanDto);
    }

    @ApiOperation(value = "根据学习计划id查询对应课程列表(小程序)", notes = "根据学习计划id查询对应课程(小程序)", httpMethod = "POST",tags = "小程序")
    @RequestMapping(value = "listCurriculumByStudyPlanId/{studyPlanId}", method = RequestMethod.POST)
    public ReturnVO<List<StudyPlanUnit>> listCurriculumByStudyPlanId(@RequestHeader String token, @ApiParam("学习计划id") @PathVariable Long studyPlanId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return studyPlanService.listCurriculumByStudyPlanId(jsonObject.getLong("id"), studyPlanId);
    }


    @ApiOperation(value = "查询学习计划详情", notes = "查询学习计划详情", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectStudyPlanDetail/{studyPlanId}", method = RequestMethod.POST)
    public ReturnVO<StudyPlanInsDto> selectStudyPlanDetail(@RequestHeader String token, @PathVariable Long studyPlanId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return studyPlanService.selectStudyPlanDetail(jsonObject.getLong("id"),studyPlanId);
    }

    @ApiOperation(value = "IM初始化", notes = "IM初始化", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "initIM/{studyPlanId}", method = RequestMethod.POST)
    public ReturnVO<Map> initIM(@PathVariable("studyPlanId") Long studyPlanId) {
        return studyPlanService.initIM(studyPlanId);
    }
}
